package pt.iul.poo.games;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import pt.iul.poo.image.MovingImage;
import pt.iul.poo.image.SimpleImage;
import pt.iul.poo.image.properties.GameWindowInterface;
import pt.iul.poo.shapes.SimpleShape;

public class DemoGame {

	private static final double RANGE = 100;
	private static final long DELAY = 100;
	private GameWindowInterface window;
	private List<MovingImage> invaders = new ArrayList<MovingImage>();
	private List<SimpleImage> towers = new ArrayList<SimpleImage>();;
	private SimpleShape shot;

	public DemoGame(GameWindowInterface window) {
		this.window = window;
	}

	private void init() {
		fakeInit();
	}

	private void fakeInit() {
		try {
			int n = window.getWidth() / window.getDefaultElementWidth();
			int m = window.getHeight() / window.getDefaultElementHeight();
			int corridorLane = n / 2;
			for (int i = 0; i != n; i++) {
				for (int j = 0; j != m; j++) {
					if (i != corridorLane) {
						window.getDisplayer().add(
								new SimpleImage("Black.png", i
										* window.getDefaultElementWidth(), j
										* window.getDefaultElementHeight(), 0));
					}
				}
			}
			SimpleImage tower = new SimpleImage("Tower1.png",
					(corridorLane - 1) * window.getDefaultElementWidth(),
					2 * window.getDefaultElementHeight(), 1);
			towers.add(tower);
			window.getDisplayer().add(tower);

			MovingImage x = new MovingImage("bug.png", corridorLane
					* window.getDefaultElementWidth(),
					0 * window.getDefaultElementHeight(), 1);
			invaders.add(x);
			window.getDisplayer().add(x);

		} catch (IOException e) {

		}

	}

	public void run() {

		init();
		window.start();
		while (!gameOver()) {
			moveCreeps();
			towerShots();
			// ...
			List<Integer> keyEvents = window.getKeyEvents();
			List<Point> mouseEvents = window.getMouseEvents();
			// ...
			if (!keyEvents.isEmpty() || !mouseEvents.isEmpty()) {
				String msg = "Keys: ";
				for (Integer i : keyEvents) {
					msg += "," + String.valueOf((char) i.intValue());
				}
				// ...
				msg += " Mouse: ";
				for (Point p : mouseEvents) {
					msg += "," + p.toString();
				}
				window.setMessage(msg);
			}
			window.getDisplayer().refresh();
			try {
				Thread.sleep(DELAY);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
		window.getDisplayer().clearImages();
		window.end();
	}

	private void towerShots() {
		if (shot != null)
			window.getDisplayer().remove(shot);

		for (SimpleImage tower : towers) {
			for (MovingImage x : invaders) {
				if (Math.abs(tower.getPosition().distance(x.getPosition())) < RANGE) {
					SimpleShape s = new SimpleShape(
							new Line2D.Double(tower.getPosition().getX()
									+ tower.getDimension().width / 2, tower
									.getPosition().getY()
									+ tower.getDimension().height / 2, x
									.getPosition().getX()
									+ x.getDimension().width / 2, x
									.getPosition().getY()
									+ x.getDimension().height / 2),
							Color.GREEN, 50);
					shot = s;
					window.getDisplayer().add(s);
				}
			}
		}

	}

	private void moveCreeps() {
		for (MovingImage x : invaders) {
			x.move(new Point(0, 1));
			if (x.getPosition().y > window.getHeight()) {
				x.getPosition().y = (int) -x.getDimension().getHeight();
			}
		}

	}

	private boolean gameOver() {
		return false;
	}

}
